﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
//using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;
using QuanLyKhachSan.DataAccess;
using QuanLyKhachSan.BusinessObject;
namespace QuanLyKhachSan.BusinessLogic
{
    class ChucVuBL
    {
        ConnectData connData = new ConnectData();
        //Lấy danh sách chức vụ
        public DataTable LayChucVu() {
            string sql = "LayChucVu";
            return connData.GetDataTable(sql);
        }

        //Tim kiem
        public DataTable TimKiemChucVu(string timKiem)
        {
            string sql = "select * from ChucVu where TenChucVu like '%" + timKiem + "%'";
            return connData.GetDataTable(sql);
        }


        //thêm mới chức vụ
        public void ThemCV(ChucVuBO cv)
        {
            SqlConnection conn =   connData.Connect1();
            conn.Open();
            string sql = "ThemMoiChucVu";
            SqlCommand objCmd = new SqlCommand(sql, conn);
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.Add("@MaChucVu", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@TenChucVu", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@TienLuong", SqlDbType.Int);

            objCmd.Parameters["@MaChucVu"].Value = cv.MaChucVu;
            objCmd.Parameters["@TenChucVu"].Value = cv.TenChucVu;
            objCmd.Parameters["@TienLuong"].Value = cv.TienLuong;
            objCmd.ExecuteNonQuery();
            conn.Close();
         }

        //
        public string NextID()
        {
            return Utilities.NextID(connData.GetLastID("ChucVu", "MaChucVu"), "CV");
        }

        public bool CheckExists(string macv)
        {
            if (connData.CheckExistValue("ChucVu", "MaChucVu", macv))
            {
                return true;
            } return false;
        }


        //sửa chức vụ
        public void SuaCV(ChucVuBO cv)
        {
                //string sql = string.Format("UPDATE ChucVu SET TenChucVu = '{0}', TienLuong = '{1}' WHERE MaChucVu = '{2}' ", cv.TenChucVu, cv.TienLuong, cv.MaChucVu);
                //if (connData.ExeCuteQuery(sql))
                //{
                //    MessageBox.Show("Sửa chức vụ thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
            SqlConnection conn = connData.Connect1();
            conn.Open();
            string sql = "SuaChucVu";
            SqlCommand objCmd = new SqlCommand(sql, conn);
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.Add("@MaChucVu", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@TenChucVu", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@TienLuong", SqlDbType.Int);

            objCmd.Parameters["@MaChucVu"].Value = cv.MaChucVu;
            objCmd.Parameters["@TenChucVu"].Value = cv.TenChucVu;
            objCmd.Parameters["@TienLuong"].Value = cv.TienLuong;
            objCmd.ExecuteNonQuery();
            conn.Close();
        }

        //xóa
        public void Delete(string cv)
        {
            string sql = "Delete from ChucVu where MaChucVu='" + cv + "'";
            connData.ExeCuteQuery(sql);
        }
    }
}
